﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AllInOne.PrintUtils;
using ArcSoftFace.Utils;
namespace AllInOne.Pages
{
    /// <summary>
    /// VerifyCodeInputPage.xaml 的交互逻辑
    /// </summary>
    public partial class VerifyCodeInputPage : Page
    {
        public VerifyCodeInputPage()
        {
            InitializeComponent();
        }

        private string lsh;

        private MainWindow parent;
        public VerifyCodeInputPage(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }


        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ResetIndex();
        }


        public void InitPage(System.Drawing.Image verifyCodeImage,string lsh)
        {
            this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));
            this.txtVerifyCode.Text = "";
            this.imgVerifyCode.Source = ImageUtil.BitmapToBitmapImage((System.Drawing.Bitmap)verifyCodeImage);
            this.lsh = lsh;
            this.parent.PlaySound(Properties.Resources.male_input_verify_code, Properties.Resources.female_input_verify_code);

        }


        private void SmallBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/small_btn.png");

        }

        private void SmallBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/small_btn_hover.png");
        }

        private void SmallBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/small_btn.png");

            TextBlock btn = (TextBlock)sender;
            string verifyCode = this.txtVerifyCode.Text;
            if (verifyCode.Length < 4)
            {
                this.txtVerifyCode.Text = verifyCode + btn.Text;
            }
        }

        private void DeleteBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/delete_btn.png");

        }

        private void DeleteBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/delete_btn_hover.png");
        }

        private void DeleteBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/delete_btn.png");
            if (this.txtVerifyCode.Text.Length > 0)
            {
                string verifyCode = this.txtVerifyCode.Text;
                this.txtVerifyCode.Text = verifyCode.Substring(0, verifyCode.Length - 1);
            }
        }

        private void ClearBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/clear_btn.png");

        }

        private void ClearBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/clear_btn_hover.png");

        }

        private void ClearBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/clear_btn.png");
            this.txtVerifyCode.Text = "";
        }

        private void BtnSubmit_MouseUp(object sender, MouseButtonEventArgs e)
        {

            if (this.txtVerifyCode.Text.Length==4)
            {
                bool loginFlag = this.parent.Login(this.txtVerifyCode.Text);
                if (!loginFlag)
                {
                    this.parent.ShowVerifyCode(this.lsh);
                    this.parent.Alert("提示", "验证失败。");
                }
                else {
                    if (this.lsh.Substring(0,1)=="2")
                    {
                        this.parent.PrintDrivingLicence(this.lsh);
                        System.Windows.Forms.Application.DoEvents();
                    }
                    else if (this.lsh.Substring(0, 1) == "1")
                    {
                        this.parent.PrintLnmj(this.lsh);
                    }
                }

            }
            else { this.parent.Alert("提示", "您输入的验证码不正确，请检查您的输入。"); }



        }


        private void setBackgroundImage(object sender, string url)
        {
            TextBlock btn = (TextBlock)sender;
            btn.Background = new ImageBrush
            {
                ImageSource = new BitmapImage(new Uri(url)),
                Stretch = Stretch.Uniform
            };
        }

    }
}
